package com.jz.community.moduleshopping.orderList.task;

import android.app.Activity;

import com.jz.community.basecomm.constant.Constant;
import com.jz.community.basecomm.net.OkHttpUtil;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.task.RxTask;
import com.jz.community.basecomm.utils.JsonUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.commview.view.widget.ProgressDialogManager;
import com.jz.community.moduleshopping.confirmOrder.bean.OrderPayResultBean;

import java.util.HashMap;

public class GroupPaymentTask extends RxTask<String, Integer, OrderPayResultBean> {


    private Activity activity;
    private ITaskCallbackListener taskListener;

    public GroupPaymentTask(Activity activity,
                            ITaskCallbackListener taskListener) {
        super(activity);
        this.activity = activity;
        this.taskListener = taskListener;
    }

    @Override
    protected OrderPayResultBean doInBackground(String... params) {
        String result = OkHttpUtil.post(Constant.GROUP_PAYMENT_URL, getParam(params[0], params[1], params[2]), true);
        OrderPayResultBean bean = null;
        if (!Preconditions.isNullOrEmpty(result)) {
            bean = JsonUtils.parseObject(result, OrderPayResultBean.class);
        }
        return bean;
    }

    @Override
    protected void onPostExecute(OrderPayResultBean result) {
        // 执行结果回调函数
        taskListener.doTaskComplete(result);
        ProgressDialogManager.dismissProgressDialog();
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        ProgressDialogManager.showDialog(activity);
        super.onPreExecute();
    }

    //微信支付类型（0-APP支付 1-小程序支付）
    private HashMap<String, Object> getParam(String orderId, String payType, String wechatType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("orderId", orderId);
        params.put("payType", payType);
        if ("2".equals(wechatType)) {
            params.put("wechatType", 0);
        } else {
            params.put("wechatType", wechatType);
        }
        params.put("openId", wechatType);
        return params;
    }
}
